package disruptor.test2;


import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @date: 2023/6/24 22:16
 */
public class TestMulti {

    public static void main(String[] args) {
        ThreadPoolExecutor executor = executor(Runtime.getRuntime().availableProcessors() + 1);
        long l = System.currentTimeMillis();
        for (int i = 0; i < 10000000; i++) {
            int j = i;
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println("consumer:" + Thread.currentThread().getName() + " Event: value=" + j + ",endOfBatch=true");
                }
            });
        }
        System.out.println(System.currentTimeMillis() - l + "-----------");
        shutdownSafe(executor);
    }

    public static ThreadPoolExecutor executor(int poolSize) {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(poolSize, poolSize,
                10, TimeUnit.SECONDS, new ArrayBlockingQueue(100, true),
                new ThreadPoolExecutor.CallerRunsPolicy());
        return executor;
    }

    public static void shutdownSafe(int delay, ThreadPoolExecutor executor) {
        executor.shutdown();
        try {
            executor.awaitTermination(delay, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("线程池已经关闭");
    }

    public static void shutdownSafe(ThreadPoolExecutor executor) {
        shutdownSafe(120, executor);
    }
}
